package com.senyint.common.ctrl;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.hp.util.IOUtil;
import com.hp.util.JsonUtil;
import com.hp.util.PropUtil;
import com.hp.util.XmlUtil;
import com.senyint.common.dao.MessageDao;
import com.senyint.common.handle.IHandle;

//@Controller
//@RequestMapping("test")
public class CommandController extends AbstractController{

    Logger logger = Logger.getLogger(this.getClass());
    
    @Autowired
    private MessageDao messageDao;
    
    
    
    private List<IHandle> handleList;
    
    
//    private List<IHandle> handleList;
//    
//    public List<IHandle> getHandleList() {
//        return handleList;
//    }
//
//    public void setHandleList(List<IHandle> handleList) {
//        this.handleList = handleList;
//    }

    
//    @RequestMapping("test")
//    public String test(){
//    	
//    	System.out.println("test");
//    	return "test";
//    }

	@Autowired
	public void setHandleList(List<IHandle> handleList) {
		this.handleList = handleList;
	}


	@SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    @RequestMapping
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        
		String encode=PropUtil.getPropByKey("add","encode")==null?"UTF-8":PropUtil.getPropByKey("add","encode");
		
		InputStream in = request.getInputStream();
		String instr = IOUtil.getStringByIn(in,"UTF-8"); //输入的
		System.out.println("接收到的流为 :"+"\n"+instr);
		Map<String,Object> map = null;
		
		if(instr.startsWith("{")){//json
			System.out.println("接收到的流类型为:json");
			map = JsonUtil.json2Map(instr);
		}else if(instr.startsWith("<")){
			System.out.println("接收到的流类型为:xml");
			map = XmlUtil.xml2Map(instr);
		}else{
			System.out.println("接收到的流类型为:不知道error");
			//.....其他情况
		}
		System.out.println("转换前的map为:"+map);
		System.out.println("转后换的map又变回xml:"+XmlUtil.map2Xml(map, "GBK","DATA"));
		
		System.out.println("messageDao:gogogo:"+messageDao);
   // 	List list = messageDao.qryNotice();
        String reAddr = request.getRemoteAddr();
        String reHost = request.getRemoteHost();
        int rePort = request.getRemotePort();
        String reUser = request.getRemoteUser();

        StringBuffer sb = new StringBuffer();
        sb.append("Got request from Address[" + reAddr + "],Host[" + reHost
                + "],Port[" + rePort + "],User[" + reUser + "]");

        logger.debug(sb.toString());
        
        Map data = map;
        
        for (IHandle iHandle : handleList) {
            iHandle.exec(data);
        }
        data.put("test","test");
        
        return new ModelAndView("test",data);
    }


}
